package com.lxmes.hr.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.hr.domain.NcPieceworkParam;
import com.lxmes.hr.domain.NcPieceworkSta;
import com.lxmes.hr.mapper.NcPieceworkStaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcPieceworkMoneyDetailMapper;
import com.lxmes.hr.domain.NcPieceworkMoneyDetail;
import com.lxmes.hr.service.INcPieceworkMoneyDetailService;

import javax.annotation.Resource;
import javax.validation.Validator;

/**
 * 计件工资明细Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-16
 */
@Service
public class NcPieceworkMoneyDetailServiceImpl implements INcPieceworkMoneyDetailService
{
    @Resource
    private NcPieceworkMoneyDetailMapper ncPieceworkMoneyDetailMapper;

    @Resource
    private NcPieceworkStaMapper ncPieceworkStaMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询计件工资明细
     *
     * @param id 计件工资明细主键
     * @return 计件工资明细
     */
    @Override
    public NcPieceworkMoneyDetail selectNcPieceworkMoneyDetailById(Long id)
    {
        return ncPieceworkMoneyDetailMapper.selectNcPieceworkMoneyDetailById(id);
    }

    /**
     * 查询计件工资明细列表
     *
     * @param ncPieceworkMoneyDetail 计件工资明细
     * @return 计件工资明细
     */
    @Override
    public List<NcPieceworkMoneyDetail> selectNcPieceworkMoneyDetailList(NcPieceworkMoneyDetail ncPieceworkMoneyDetail)
    {
        return ncPieceworkMoneyDetailMapper.selectNcPieceworkMoneyDetailList(ncPieceworkMoneyDetail);
    }

    /**
     * 新增计件工资明细
     *
     * @param ncPieceworkMoneyDetail 计件工资明细
     * @return 结果
     */
    @Override
    public int insertNcPieceworkMoneyDetail(NcPieceworkMoneyDetail ncPieceworkMoneyDetail)
    {
        ncPieceworkMoneyDetail.setCreateTime(DateUtils.getNowDate());
        return ncPieceworkMoneyDetailMapper.insertNcPieceworkMoneyDetail(ncPieceworkMoneyDetail);
    }

    /**
     * 修改计件工资明细
     *
     * @param ncPieceworkMoneyDetail 计件工资明细
     * @return 结果
     */
    @Override
    public int updateNcPieceworkMoneyDetail(NcPieceworkMoneyDetail ncPieceworkMoneyDetail)
    {
        ncPieceworkMoneyDetail.setUpdateTime(DateUtils.getNowDate());
        return ncPieceworkMoneyDetailMapper.updateNcPieceworkMoneyDetail(ncPieceworkMoneyDetail);
    }

    /**
     * 批量删除计件工资明细
     *
     * @param ids 需要删除的计件工资明细主键
     * @return 结果
     */
    @Override
    public int deleteNcPieceworkMoneyDetailByIds(Long[] ids)
    {
        return ncPieceworkMoneyDetailMapper.deleteNcPieceworkMoneyDetailByIds(ids);
    }

    /**
     * 删除计件工资明细信息
     *
     * @param id 计件工资明细主键
     * @return 结果
     */
    @Override
    public int deleteNcPieceworkMoneyDetailById(Long id)
    {
        return ncPieceworkMoneyDetailMapper.deleteNcPieceworkMoneyDetailById(id);
    }

    @Override
    public String imporIteminfo(List<NcPieceworkMoneyDetail> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcPieceworkMoneyDetail org : userList)
        {
            try
            {
                BeanValidators.validateWithException(validator, org);
                org.setCreateBy(operName);
                org.setUpdateBy(operName);
                this.insertNcPieceworkMoneyDetail(org);
                successNum++;
                successMsg.append("<br/>" + successNum + "、员工工号 " + org.getJobNo() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + org.getJobNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int readTotal() {
        List<NcPieceworkSta> ncPieceworkStas = ncPieceworkStaMapper.selectNcPieceworkStaConnectParam(null);
        if(ncPieceworkStas.size()!=0){
            ncPieceworkStas.parallelStream().forEach(p->{
                NcPieceworkMoneyDetail detail = new NcPieceworkMoneyDetail();
                detail.setGcCode(p.getGcCode());
                detail.setGcName(p.getGcName());
                detail.setPieceworkDate(p.getPieceworkDate());
                detail.setName(p.getName());
                detail.setJobNo(p.getJobNo());
                detail.setDepartment(p.getDepartment());
                detail.setBranch(p.getBranch());
                detail.setpGroup(p.getPGroup());
                detail.setJobs(p.getJobs());
                detail.setCj(p.getCj());
                detail.setProLine(p.getProLine());
                detail.setGwCode(p.getGwCode());
                detail.setGwName(p.getGwName());
                detail.setOrderNo(p.getOrderNo());
                detail.setItemCode(p.getItemCode());
                detail.setItemName(p.getItemName());
                detail.setPieceworkUnit(p.getPieceworkUnit());
                detail.setPieceworkPrice(p.getPieceworkPrice()==null?new BigDecimal(0):p.getPieceworkPrice());
                detail.setPieceworkNum(p.getBgNum());
                detail.setPieceworkGz(detail.getPieceworkNum().multiply(detail.getPieceworkPrice()));
                detail.setPieceworkGz(detail.getPieceworkNum().multiply(detail.getPieceworkPrice()));
                detail.setCreateBy(SecurityUtils.getUsername());
                detail.setCreateTime(DateUtils.getNowDate());
            });
        }
        return 1;
    }
}
